package cms.models {
  import org.restfulx.collections.ModelsCollection;
  import org.restfulx.models.RxModel;
  
  [Resource(name="deposits")]
  [Bindable]
  public class Deposit extends RxModel {
    public static const LABEL:String = "label";
    public static const ANY:Deposit = new Deposit();

    public var label:String = "";

    public var date:Date = new Date;

    public var closed:Boolean = false;
    
    [Ignored]
    public var totalDeposit:Number = 0;

    [HasMany]
    public var payments:ModelsCollection;
    
    public function Deposit() {
      super(LABEL);
    }

    [Ignored]
    public function get computedTotalDeposit():Number {
	var total:Number = new Number(0);
	for each (var payment:Payment in payments) {
	    total += payment.amount;
	};
	return total;
    }

    public function get computedTotalFunds():Object {
	var totalTitheOffering:Number = new Number(0);
	var totalMissions:Number = new Number(0);
        var totalRiverBend:Number = new Number(0);
	var totalOther:Number = new Number(0);
	for each (var payment:Payment in payments) {
	    if (payment.fund.substring(0,1) == "T") {
		totalTitheOffering += payment.amount;
	    }
	    else if (payment.fund.substring(0,1) == "M") {
		totalMissions += payment.amount;
	    }
	    else if (payment.fund.substring(0,1) == "R") {
		totalRiverBend += payment.amount;
	    }
	    else if (payment.fund.substring(0,1) == "O") {
		totalOther += payment.amount;
	    };
	};
	return {
	    tithe_offering: totalTitheOffering,
	    missions: totalMissions,
	    river_bend: totalRiverBend,
	    other: totalOther
	};
    }

  }
}
